package 每日一题;

import java.util.HashMap;
import java.util.List;

/**
 * @author chenhong
 * @version 1.0
 * @description TODO
 * @date 2023/4/29 15:42
 */
public class L2423 {
    public static void main(String[] args) {
        new L2423().equalFrequency("abbcc");
    }

    public boolean equalFrequency(String word) {
        int[] count = new int[26];
        for (int i = 0; i < word.length(); i++) {
            count[word.charAt(i) - 'a']++;
        }
        int key1 = -1,key2 = -1;
        int []frequent = new int[2];
        for (int i = 0; i < count.length; i++) {
            if(count[i] != 0){
                if(key1 == -1){
                    key1 = count[i];
                    frequent[0] ++;
                }else if(count[i] == key1){
                    frequent[0] ++;
                }else if(key2 == -1){
                    key2 = count[i];
                    frequent[1] ++;
                }else if(count[i] == key2){
                    frequent[1]++;
                }else {
                    return false;
                }
            }
        }
        if(key2 == -1 && (key1 == 1 || frequent[0] == 1))
            return true;
        if(key2 != -1){
            if(key1 == 1 && frequent[0] == 1 || key2 == 1 && frequent[1] == 1)
                return true;
            if(key1 > key2 && frequent[0] == 1 && key1 - key2 == 1)
                return true;
            if(key1 < key2 && frequent[1] == 1&& key2 - key1 == 1)
                return true;
            if(key1 == key2 && key1 == 1)
                return true;
        }
        return false;
    }
}
